﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.Business;
using UFSoft.UBF.MD;
using UFSoft.UBF.MD.EntityModel;
using UFSoft.UBF.MD.EntityModelImp;
using UFSoft.UBF.PL.MetaDataWrap;
using UFSoft.UBF.Util.Data;

namespace UFIDA.U9.CusDev.AITPlugInBE
{
    internal class EntityUtils
    {
        /// <summary>
        /// 获取子实体属性名称集合
        /// 该方法使用的标准API,通过查询元数据的组合关系获取
        /// 使用 EntityTools 获取关联关系
        /// </summary>
        /// <param name="entity">父实体</param>
        /// <returns>父实体关联子实体的属性名集合</returns>
        public static List<string> getAssociation(BusinessEntity entity)
        {
            List<string> lines = new List<string>();

            IAssociationCollection asslist = EntityTools.GetAllAssoByParentEntity(entity.MDEntity);
            //这是获取实体元数据的另一个方法
            //IAssociationCollection typeAllAssociation = MetaDataUtil.GetTypeAllAssociation(entity.MDEntity);
            foreach (var ass in asslist)
            {
                Association ass1 = (Association)ass;
                if(ass1.Kind== EnumAssociationKind.COMPOSITION)
                {
                    //获取 组合关系 中,父属性名称
                    lines.Add(ass1.ParentMember.Name);
                }
            }

            return lines;

        }
        /// <summary>
        /// 获取子实体属性名称集合
        /// 该方法是通过调试跟踪编写的代码,不是很准确
        /// </summary>
        /// <param name="entity">父实体</param>
        /// <returns>父实体关联子实体的属性名集合</returns>
        public static List<string> getLines(BusinessEntity entity)
        {
            List<string> lines = new List<string>();
            //通过反射从Relations中获取子实体
            Type type = entity.Relations.GetType();
            object _relations = type
                .GetField("_relations",BindingFlags.NonPublic|BindingFlags.Instance)
                .GetValue(entity.Relations);
            HybridDictionary _dict = _relations.GetType()
                .GetField("_dict", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(_relations) as HybridDictionary;
            if (_dict == null)
            {
                return lines;
            }

            foreach (var key in _dict.Keys)
            {
                object value = _dict[key];
                if (value.GetType().Name == "StrongRelationReference")
                {
                    //只返回组合关系
                    lines.Add(key.ToString());
                }
            }
            return lines;
        }

        /// <summary>
        /// 通过弱类型获取实体DocNo或Code
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string getCode(BusinessEntity entity)
        {
            string code = "";
            if (entity.Values.Contains("DocNo"))
            {
                code = entity.Values["DocNo"].ToString();
            }
            else if (entity.Values.Contains("Code"))
            {
                code = entity.Values["Code"].ToString();
            }
            return code;
        }
        /// <summary>
        /// 通过弱类型获取实体名称
        /// 涉及多语处理
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static string getName(BusinessEntity entity)
        {
            string name = "";

            if (entity.Values.Contains("Name"))
            {
                object obj = entity.Values["Name"];
                if (obj == null) return name;

                if (obj is MultiLangDataDict)
                {
                    name=((MultiLangDataDict)obj).ToSimpleString();
                }
                else
                {
                    name = obj.ToString();
                }

            }
            return name;
        }
    }
}
